//获取应用实例
var app = getApp()

var CommonUtils = app.CommonUtils;
var Keys = require("../../assets/data/keys.js")
const Toptips = require('../../dist/toptips/index');

Page(Object.assign({}, Toptips, {
	/**
	 * A_Address:"她"
	 * A_City:"漯河市"
	 * A_District:"舞阳县"
	 * A_ID:"262"
	 * A_Istrue:"0"
	 * A_Province:"河南省"
	 * A_Username:"哦哦"
	 * A_Usertel:"18366184127"
	 * IsDefaultAddress:false  //此属性自己定义
	 * HP_AreaLine:'-'
	 */
	/**
	 * 页面的初始数据
	 */
	data: {
		IsEditAddressState: false,////是否是编辑地址状态

		A_ID: '',
		A_Username: '',
		A_Usertel: '',
		A_Province: '',
		A_CIty: '',
		A_District: '',
		A_Address: '',
		A_Line: '',
		A_Istrue: false,//是否是默认地址

		SelectCityChangeValue: [0, 0, 0],//picker-view 内的 picker-view-column 选择的第几项（下标从 0 开始）
		PCCArrayList: [],//所有省市区的数据
		SelectProvinceArray: [],//省列表数组
		SelectCityArray: [],//市列表数组
		SelectCountyArray: [],//区列表数组
		OpenSelectAddress: false,//打开地址选择框
	},

	/**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		var _this = this;
		_this.requestNetAllCity();
		if (options.EditAddressInfo) {//判断是修改地址还是新建地址
			var tempEditAddressInfo = JSON.parse(options.EditAddressInfo);
			_this.setData({
				IsEditAddressState: true,
				A_ID: tempEditAddressInfo.A_ID,
				A_Username: tempEditAddressInfo.A_Username,
				A_Usertel: tempEditAddressInfo.A_Usertel,
				A_Province: tempEditAddressInfo.A_Province,
				A_CIty: tempEditAddressInfo.A_CIty,
				A_District: tempEditAddressInfo.A_District,
				A_Address: tempEditAddressInfo.A_Address,
				A_Line: ' - ',
				A_Istrue: tempEditAddressInfo.A_Istrue == '1',
			})
			wx.setNavigationBarTitle({
				title: '编辑地址'
			})
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 网络请求--全国所有城市
	 */
	requestNetAllCity: function () {
		var _this = this;
		wx.showLoading()
		wx.request({
			url: 'https://www.chnode.xyz/api/Address.ashx?DataType=AllCity',
			success: function (res) {
				if (res.data.isok == "true") {
					_this.initAllCityList(res.data.datas)
				} else {
					_this.showZanTopTips(res.data.info);
				}
			},
			complete: function () { wx.hideLoading() },
			fail: function () { _this.showZanTopTips("网络错误"); }
		})
	},

	/**
	 * 初始化地址选择数据
	 */
	initAllCityList: function (datas) {
		var _this = this;
		_this.data.PCCArrayList = datas;
		for (let i = 0, j = datas.length; i < j; i++) {
			_this.data.SelectProvinceArray.push(datas[i].Province);
		}
		if (!_this.data.IsEditAddressState) {
			for (let i = 0, j = datas[0].City.length; i < j; i++) {
				_this.data.SelectCityArray.push(datas[0].City[i].citys)
			}
			for (let i = 0, j = datas[0].City[0].District.length; i < j; i++) {
				_this.data.SelectCountyArray.push(datas[0].City[0].District[i].districts)
			}
			_this.setData({
				SelectProvinceArray: _this.data.SelectProvinceArray,
				SelectCityArray: _this.data.SelectCityArray,
				SelectCountyArray: _this.data.SelectCountyArray,
			})
		} else {
			console.log(_this.data.A_Province)
			var indexProvince = _this.data.SelectProvinceArray.indexOf(_this.data.A_Province);
			for (let i = 0, j = datas[indexProvince].City.length; i < j; i++) {
				_this.data.SelectCityArray.push(datas[indexProvince].City[i].citys)
			}
			var indexCity = _this.data.SelectCityArray.indexOf(_this.data.A_CIty);
			for (let i = 0, j = datas[indexProvince].City[indexCity].District.length; i < j; i++) {
				_this.data.SelectCountyArray.push(datas[indexProvince].City[indexCity].District[i].districts)
			}
			var indexCounty = _this.data.SelectCountyArray.indexOf(_this.data.A_District);
			_this.setData({
				SelectProvinceArray: _this.data.SelectProvinceArray,
				SelectCityArray: _this.data.SelectCityArray,
				SelectCountyArray: _this.data.SelectCountyArray,
			})
			_this.bindSelectCityChange({ detail: { value: [indexProvince, indexCity, indexCounty] } })
		}
	},

	/**
	 * 地区选择器绑定事件
	 */
	bindSelectCityChange: function (e) {
		var _this = this;
		var index0 = e.detail.value[0], index1 = e.detail.value[1], index2 = e.detail.value[2];
		var SCCV = _this.data.SelectCityChangeValue;
		var PCAL = _this.data.PCCArrayList;

		if (index0 != SCCV[0]) {
			_this.data.SelectCityArray = [];
			_this.data.SelectCountyArray = [];
			for (let i = 0, j = PCAL[index0].City.length; i < j; i++) {
				_this.data.SelectCityArray.push(PCAL[index0].City[i].citys)
			}
			for (let i = 0, j = PCAL[index0].City[0].District.length; i < j; i++) {
				_this.data.SelectCountyArray.push(PCAL[index0].City[0].District[i].districts)
			}
			this.setData({
				SelectCityArray: _this.data.SelectCityArray,
				SelectCountyArray: _this.data.SelectCountyArray,
				SelectCityChangeValue: [index0, 0, 0],
			})
		}
		if (index1 != SCCV[1]) {
			_this.data.SelectCountyArray = [];
			for (let i = 0, j = PCAL[index0].City[index1].District.length; i < j; i++) {
				_this.data.SelectCountyArray.push(PCAL[index0].City[index1].District[i].districts)
			}
			this.setData({
				SelectCountyArray: _this.data.SelectCountyArray,
				SelectCityChangeValue: [index0, index1, 0]
			})
		}
		if (index2 != SCCV[2]) {
			this.setData({
				SelectCityChangeValue: [index0, index1, index2]
			})
		}
	},

	/**
	 * 默认地址选择的监听事件
	 */
	btnEventIsDefaultAddress: function (e) {
		this.setData({
			A_Istrue: !this.data.A_Istrue,
		})
	},

	/**
	 * 打开地址选择
	 */
	btnEventOpenSelectAddress: function () {
		var _this = this;
		_this.setData({
			OpenSelectAddress: !_this.data.OpenSelectAddress,
		})
	},

	/**
	 * 地址选择取消
	 */
	btnEventSelectACancel: function () {
		var _this = this;
		_this.setData({
			OpenSelectAddress: !_this.data.OpenSelectAddress,
		})
	},
	/**
	 * 地址选择确认
	 */
	btnEventSelectAConfirm: function () {
		var _this = this;
		var index0 = _this.data.SelectCityChangeValue[0], index1 = _this.data.SelectCityChangeValue[1], index2 = _this.data.SelectCityChangeValue[2];
		// _this.data.A_Province = _this.data.PCCArrayList[index0].Province;
		// _this.data.A_CIty = _this.data.PCCArrayList[index0].City[index1].citys;
		// _this.data.A_District = _this.data.PCCArrayList[index0].City[index1].District[index2].districts;
		// _this.data.HP_AreaLine = ' - ';
		_this.setData({
			OpenSelectAddress: !_this.data.OpenSelectAddress,
			A_Province: _this.data.PCCArrayList[index0].Province,
			A_CIty: _this.data.PCCArrayList[index0].City[index1].citys,
			A_District: _this.data.PCCArrayList[index0].City[index1].District[index2].districts,
			A_Line: ' - ',
		})
	},


	/**
	 * 取消的点击事件
	 */
	bindEventCancel: function () {
		wx.navigateBack({})
	},

	/**
	 * 保存地址点击事件
	 */
	btnEventSaveAddress: function (e) {
		var _this = this;
		console.log(e)
		var name = e.detail.value.Name;
		var phone = e.detail.value.Phone;
		var area = e.detail.value.Area;
		var areaArray = area.split(' - ');
		var address = e.detail.value.Address;
		 var isDefault = _this.data.A_Istrue == true ? '1' : '0';
		var province = areaArray[0];
		var city = areaArray[1];
		var district = areaArray[2];

		if (name == "") {
			_this.showZanTopTips("请填写联系人姓名");
			return
		}
		if (!CommonUtils.isPoneAvailable(phone)) {
			_this.showZanTopTips("请输入有效的手机号");
			return;
		}

		if (area == "") {
			_this.showZanTopTips("请选择省市区");
			return
		}

		if (address == "") {
			_this.showZanTopTips("请填写详细地址");
			return
		}

		_this.requestNetSaveAddress(province, city, district, address, name, phone, isDefault)
	},

	/**
	 * 网络请求--保存地址
	 */
	requestNetSaveAddress: function (tProvince, tCity, tCountry, tStreet, tName, tTel, tIsDefault) {
		var _this = this;
		var Save_Type = 'https://www.chnode.xyz/api/Address.ashx?' + (_this.data.IsEditAddressState ? ('DataType=AddressUpdate&A_ID=' + _this.data.A_ID) : 'DataType=AddressAdd');
		wx.request({
			url: Save_Type,
			data: {
				'Token': getApp().data.Token,
				'Province': tProvince,
				'City': tCity,
				'Country': tCountry,
				'Street': tStreet,
				'Name': tName,
				'Tel': tTel,
				'IsDefault': tIsDefault
			},
			success: function (res) {
				console.log('success', res.data)
				wx.showToast({
					title: res.data.info,
					image: res.data.isok == 'true' ? '/images/icon/icon-right.png' : '/images/icon/icon-error.png',
					duration: 2000,
				})
				setTimeout(function () {
					wx.navigateBack({
						delta: 1
					})
				}, 2000) //延迟时间 这里是1秒  
			},
			fail: function (res) {
				console.log('fail', res)
			}
		})
	},
}))

// http://172.18.1.155:8017/api/Address.ashx?DataType=AddressUpdate&Token=123456&Province=黑龙江省&City=热河市&Country=龙江县&Street=援朝街道203户&Name=刘伟 &Tel=13344667891&IsDefault=1&A_ID=7
